package com.tianshi.pxx.controller.admin;


import com.tianshi.pxx.pojo.entity.Product;
import com.tianshi.pxx.pojo.entity.User;
import com.tianshi.pxx.pojo.vo.ProductVO;
import com.tianshi.pxx.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    /*
    * 将商品增加到购物车
    * */
    @PostMapping("/add/{productId}") //这里指定后,PathVariable里面需要写吗?
    public ResponseEntity<String> add(@PathVariable("productId") Integer productId, HttpSession session) {
        log.info("add product to cart, productId:{}", productId);
        Integer userId = userId(session); //获取当前用户id
        cartService.add(productId,userId);
        return ResponseEntity.ok("success");
    }

    /*
    * 查询购物车中的商品
    * */
    @GetMapping("/list")
    public ResponseEntity<List<ProductVO>> list(HttpSession session) {
        log.info("list cart");
        Integer userId = userId(session); //获取当前用户id
        //查询购物车中的商品
        List<ProductVO> res=cartService.list(userId);
        log.info("cart list:{}", res);
        return ResponseEntity.ok(res);
    }

    /*
    * 根据cartId删除购物车中的商品
    * */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) {
        log.info("delete product from cart, id:{}", id);
        //删除购物车中的商品
        cartService.delete(id);
        return ResponseEntity.ok("success");
    }

    /*
    * 清空购物车
    * */
    @DeleteMapping("/clear")
    public ResponseEntity<String> clear(HttpSession session) {
        log.info("clear cart");
        Integer userId = userId(session); //获取当前用户id


        cartService.clear(userId);
        return ResponseEntity.ok("success");
    }

    /*
    * 对在购物车中的商品添加一个
    * */
    @PutMapping("/addCount/{id}")
    public ResponseEntity<String> addCount(@PathVariable("id") Integer id) {
        log.info("add count to product in cart, id:{}", id);
        //添加一个商品到购物车
        cartService.addCount(id);
        return ResponseEntity.ok("success");
    }

    /*
    * 对在购物车中的商品减少一个
    * */
    @PutMapping("/subCount/{id}")
    public ResponseEntity<String> subCount(@PathVariable("id") Integer id) {
        log.info("sub count to product in cart, id:{}", id);
        //减少一个商品到购物车
        cartService.subCount(id);
        return ResponseEntity.ok("success");
    }

    /*
    * 购物车结算
    * 清空购物车, 并将商品加入订单表
    * */
    @PostMapping("/checkout")
    public ResponseEntity<String> checkout(HttpSession session) {
        log.info("checkout cart");
        Integer userId = userId(session); //获取当前用户id

        cartService.checkout(userId);
        return ResponseEntity.ok("success");
    }

    private Integer userId(HttpSession session){
        //获取当前用户的id
        User user = (User)session.getAttribute("currentUser");
        //用户id
        Integer userId=user.getId();
        return userId;
    }

}
